// pages/space/space.js
const app = getApp();
const db = wx.cloud.database();
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0';
var openid = "";
var otherid = "";
var avatar = ""
var nickname = ""
var signature = ""
Page({

    /**
     * 页面的初始数据
     */
    data: {

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that = this;
        openid = app.globalData.openid;
        const eventChannel = this.getOpenerEventChannel();
        eventChannel.on('acceptDataFromOpenerPage', function(data) {
            otherid = data.otherid;
            avatar = data.avatar
            that.setData({
                avatarUrl: defaultAvatarUrl,
                nickName: "加载中...",
                signature: "加载中..."
                
            })
            that.getAvatar();
            that.getStatistic();
        });
    },
    getAvatar(){
        let that = this;
        db.collection("infoDB").where({
            _openid: otherid
        }).get({
            success: function(res){
                nickname = res.data[0].nickname;
                if ( res.data[0].avatar )
                    avatar = res.data[0].avatar;
                else
                    avatar = defaultAvatarUrl;
                signature = res.data[0].signature;
                that.setData({
                    nickName: nickname,
                    nickNameLen: nickname.length,
                    avatarUrl: avatar,
                    signature: signature
                })
            },
        });
    },
    getStatistic(){
        let that = this;
        db.collection("diaryDB").where({
            _openid: otherid
        }).get({
            success: function(res){
                let data = res.data;
                let diaryDict = {};
                let pics = 0;    
                for ( let i in data ) {
                    let date = String(data[i].year) + String(data[i].month) + String(data[i].day);
                    diaryDict[date] = 1;
                    for ( let j in data[i].cloudPath )
                        pics += 1;
                }
                let diarys = data.length;
                let days = Object.keys(diaryDict).length;
                that.setData({
                    diarys: diarys,
                    days: days,
                    pics: pics
                });
            }
        })
        db.collection("experienceDB").where({
            _openid: otherid
        }).get({
            success: function(res){
                let exp = res.data;
                let like = 0;
                let favourite = 0;
                for ( let i in exp ) {
                    like += Object.keys(exp[i]["like"]).length;
                    favourite += Object.keys(exp[i]["favourite"]).length;
                }
                that.setData({
                    exp: exp.length,
                    like: like,
                    favourite: favourite
                });
            },
            fail: function(res) {
                console.log(res)
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})